package com.oracle.lingshangejia.base;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.utils.UIUtils;


/**
 * 对话框的基类
 */

public abstract class BaseDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_NoActionBar);
        // 关闭标题栏，setContentView() 之前调用
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(initView());
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        initWindow(window);
        return dialog;
    }


    private View initView() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(setLayoutId(), null);
        initData(view);
        return view;
    }


    /**
     * @param window  窗体
     * @param gravity 对齐方式  {@link Gravity#CENTER}
     * @param width  宽度         {@link WindowManager.LayoutParams#WRAP_CONTENT}
     * @param height 高度
     * @param x   左上角x轴偏移量
     * @param y   左上角y轴偏移量
     */
    protected void setLayoutParams(Window window, int gravity,int width,int height,int x,int y) {

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = gravity;
        layoutParams.width = width == WindowManager.LayoutParams.WRAP_CONTENT
                || width == WindowManager.LayoutParams.MATCH_PARENT
                ? width: UIUtils.dip2Px(getContext(),width);

        layoutParams.height = height == WindowManager.LayoutParams.WRAP_CONTENT
                || height == WindowManager.LayoutParams.MATCH_PARENT
                ? height:UIUtils.dip2Px(getContext(),height);
        layoutParams.y = UIUtils.dip2Px(getContext(),y);
        //适配华为软键盘
        if (gravity == Gravity.BOTTOM && height!= WindowManager.LayoutParams.WRAP_CONTENT &&
                height != WindowManager.LayoutParams.MATCH_PARENT) {
            layoutParams.y = layoutParams.y + UIUtils.getDaoHangHeight(getContext());
        }

        setDefaultBackground(window, R.color.transparent);


    }

    @NonNull
    protected void setDefaultBackground(Window window, int resId) {
        window.setBackgroundDrawableResource(resId);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.dimAmount = 0.5f;//使背景变暗
        window.setAttributes(layoutParams);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//使背景变暗
        window.setAttributes(layoutParams);
    }

    /**
     * @return
     * 导入布局
     */
    public abstract int setLayoutId();

    /**
     * @param window 设置window的参数参数
     * 一般调用{@link #setLayoutParams(Window, int, int, int, int, int)}方法
     */
    protected abstract void initWindow(Window window);

    /**
     * @param view 根布局
     */
    protected void initData(View view) {
    }

}
